package com.codi.fcloud.ufx.hssale.portfolio;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.HsUtil;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.PortfolioSummaryQryDto;
import com.codi.trade.response.HsPortfolioSummaryQryResponse;
import com.codi.trade.service.portfolio.HsPortfolioSummaryQryService;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组合投资交易查询
 *
 * @author spy
 * @version 1.0 2017-11-06 13:06
 * @since 1.0
 */
@Slf4j
@Service
public class HsPortfolioSummaryQryServiceImpl extends UfxSDKFactory implements HsPortfolioSummaryQryService {

    @Loggable
    @Override
    public Map<String, Object> query(PortfolioSummaryQryDto dto) {

        Preconditions.checkNotNull(dto);
        Preconditions.checkNotNull(dto.getFund_busin_code());
        Preconditions.checkState(!(Strings.isNullOrEmpty(dto.getTrade_acco())
                && Strings.isNullOrEmpty(dto.getClient_id())),
            "交易账号与客户编号至少有一个为必填项");


        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", trustWay);
        request.put("net_no", getString(dto.getNet_no(), "0001"));


        request.put("request_num", getString("50", "50")); //	N8	请求行数	Y	最大不能超过50；即最大返回记录条数不会超过50条；
        request.put("reqry_recordsum_flag", getString(dto.getReqry_recordsum_flag(), "1"));//	C1	重新统计总记录数标志	Y	0-不重新统计；1-重新统计；
        request.put("sort_direction", getString(dto.getSort_direction()));//	C1	返回排序方式	N
        request.put("trade_acco", getString(dto.getTrade_acco())); //	C17	交易账号	N
        request.put("ta_acco", getString(dto.getTa_acco()));//	C12	TA账号	N
        request.put("client_id", getString(dto.getClient_id()));//	C18	客户编号	N	交易账号与客户编号至少有一个为必填项

        //	N8	查询起始行号	N	比如总记录是100，每50条分页；那么需要查询2次；第1次查询请求行数传50，重新统计总记录数传1，查询起始行号private String 传1；第2次查询请求行数传50，重新统计总记录数传0，查询起始行号传51；
        request.put("qry_beginrownum", getString(dto.getQry_beginrownum()));

        request.put("fund_busin_code", getString(dto.getFund_busin_code()));//	C3	业务代码	N

        request.put("portfolio_allot_no", getString(dto.getPortfolio_allot_no()));//	C24	组合申请编号	N
        request.put("begin_date", getString(dto.getBegin_date()));//	C8	开始下单日期	N
        request.put("end_date", getString(dto.getEnd_date()));//	C8	结束下单日期	N


        try {
            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);
            // 如果requestNum是我们系统定义的最大值，则表明数据需要全部取出来
            getNextResponse(request, response, dto.getRequest_num());
            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    @Override
    @Loggable
    public List<HsPortfolioSummaryQryResponse> queryV2(PortfolioSummaryQryDto dto) {
        Preconditions.checkNotNull(dto);


        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", trustWay);
        request.put("net_no", getString(dto.getNet_no(), "0001"));


        request.put("request_num", getString("50", "50")); //	N8	请求行数	Y	最大不能超过50；即最大返回记录条数不会超过50条；
        request.put("reqry_recordsum_flag", getString(dto.getReqry_recordsum_flag(), "1"));//	C1	重新统计总记录数标志	Y	0-不重新统计；1-重新统计；
        request.put("sort_direction", getString(dto.getSort_direction()));//	C1	返回排序方式	N
        request.put("trade_acco", getString(dto.getTrade_acco())); //	C17	交易账号	N
        request.put("ta_acco", getString(dto.getTa_acco()));//	C12	TA账号	N
        request.put("client_id", getString(dto.getClient_id()));//	C18	客户编号	N	交易账号与客户编号至少有一个为必填项

        //	N8	查询起始行号	N	比如总记录是100，每50条分页；那么需要查询2次；第1次查询请求行数传50，重新统计总记录数传1，查询起始行号private String 传1；第2次查询请求行数传50，重新统计总记录数传0，查询起始行号传51；
        request.put("qry_beginrownum", getString(dto.getQry_beginrownum()));

        request.put("fund_busin_code", getString(dto.getFund_busin_code()));//	C3	业务代码	N

        request.put("portfolio_allot_no", getString(dto.getPortfolio_allot_no()));//	C24	组合申请编号	N
        request.put("begin_date", getString(dto.getBegin_date()));//	C8	开始下单日期	N
        request.put("end_date", getString(dto.getEnd_date()));//	C8	结束下单日期	N


        try {
            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);
// 如果requestNum是我们系统定义的最大值，则表明数据需要全部取出来
            getNextResponse(request, response, dto.getRequest_num());
            return HsUtil.convertList(appendLog(request, response), HsPortfolioSummaryQryResponse.class);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    @Override
    public PortfolioSummaryQryDto getDto(String net_no, Integer request_num, String reqry_recordsum_flag, String qry_beginrownum, String sort_direction, String trade_acco, String ta_acco, String client_id, String begin_date, String end_date, String fund_busin_code, String portfolio_allot_no) {
        PortfolioSummaryQryDto portfolioSummaryQryDto = new PortfolioSummaryQryDto();
        portfolioSummaryQryDto.setBegin_date(begin_date);
        portfolioSummaryQryDto.setClient_id(client_id);
        portfolioSummaryQryDto.setEnd_date(end_date);
        portfolioSummaryQryDto.setNet_no(net_no);
        portfolioSummaryQryDto.setQry_beginrownum(qry_beginrownum);
        portfolioSummaryQryDto.setReqry_recordsum_flag(reqry_recordsum_flag);
        portfolioSummaryQryDto.setRequest_num(request_num);
        portfolioSummaryQryDto.setSort_direction(sort_direction);
        portfolioSummaryQryDto.setTa_acco(ta_acco);
        portfolioSummaryQryDto.setTrade_acco(trade_acco);
        portfolioSummaryQryDto.setFund_busin_code(fund_busin_code);
        portfolioSummaryQryDto.setPortfolio_allot_no(portfolio_allot_no);
        return portfolioSummaryQryDto;
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.PORTFOLIO_SUMMARY_QRY;
    }
}
